package com.kzw.oa.comm.utils;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;

public class DebugConnection implements InvocationHandler {

	private Connection conn = null;

	public DebugConnection(Connection conn) {
		this.conn = conn;
	}

	public Connection getConnection() {
		return (Connection) Proxy.newProxyInstance(conn.getClass()
				.getClassLoader(), conn.getClass().getInterfaces(), this);
	}

	public Object invoke(Object proxy, Method m, Object[] args)
			throws Throwable {
		try {
			String method = m.getName();
			if ("prepareStatement".equals(method)
					|| "createStatement".equals(method))
				System.out.println("[SQL] >>> " + args[0]);
			return m.invoke(conn, args);
		} catch (InvocationTargetException e) {
			throw e.getTargetException();
		}
	}
}
